<?php
/**
 * Created by PhpStorm.
 * User: krystal
 * Date: 2019/4/22
 * Time: 18:08
 */

namespace app\common\model;

use XznsTools\cache\CacheManage;

class ZlAstrExercise extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.zl_astr_exercise';
    protected $cacheModel = 'astr_exercise_info';

    /**
     * 缓存获取类型下的所有考核题目信息
     * @return array
     * @throws \Exception
     */
    public function getExerciseInfo($type, $num = 45)
    {
        $ids = CacheManage::getInstance()->setCacheConfig($this->cacheModel)
            ->setListKeyType($type)
            ->ZsGetIocData('list', function () use ($type)
            {
                return $this->getExerciseIdFromDms($type);
            });
        if ($ids)
        {
            if (count($ids) > 45)
            {
                //获取随机的key
                $ids = array_rand($ids, $num);
            }
            $data = $this->batchGetInfoByIds($ids, 'id,title,t_id,title_img,,option_a,option_b,option_c,option_d,answer,option_type,extend');
            $data = array_values($data);
        } else
        {
            $data = [];
        }

        return $data;
    }

    /**
     * 数据库获取类型下的所有考核题目id
     * @return array
     */
    public function getExerciseIdFromDms($type)
    {
        $where = ['t_id' => $type, 'status' => 1];
        try
        {
            $ids = $this->where($where)->order('id DESC')->column('id', 'id');
            $ids = array_flip_key_value($ids);
        }
        catch (\Exception $e)
        {
            $ids = [];
        }

        return $ids;
    }

    /**
     * 数据库批量获取考核题目信息
     * @param $ids
     * @param $type
     * @param $sortType
     * @return array|void
     */
    public function getInfoByIdsInData($ids, $type, $sortType)
    {
        $where = ['status' => 1, 'id' => ['in', $ids]];
        try
        {
            $data = $this->where($where)->order('id', 'DESC')->column('id,title,t_id,title_img,option_a,option_b,option_c,option_d,answer,option_type,extend', 'id');
        }
        catch (\Exception $e)
        {
            $data = [];
        }

        return $data;
    }
}